package com.lzx.adx.report.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzs.adx.commmon.redis.RedisUtils;
import com.lzs.adx.commmon.utils.Result;
import com.lzx.adx.report.constant.RedisCacheKey;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;

@Component
@EnableScheduling
@Slf4j
public class WeiBoJOb {


    @Autowired
    private ReportService reportService;

    @Autowired
    private RedisUtils redisUtils;


    @Scheduled(cron = "0 */10 * ? * *")
    public void reportEvent() {
        List<Object> list = redisUtils.getList(RedisCacheKey.qimao_callback_key);
        if(CollectionUtil.isEmpty(list)){return;}
        list.forEach(item -> {
            Map<String,Object> params=new HashMap();
            params.put("bizRequestId",item.toString());
            this.callback(item.toString(),"3001");
        });
    }



    public ResponseEntity<?> callback(String requestId, String event_type) {
        log.info("接收了七猫回调信息，请求流水号{},回调类型为{}", requestId, event_type);
        log.info("reportService---"+reportService);
        //1.把转换结果更新到数据库
        Map<String,Object> params=new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        ReportDTO reportDTO;
        if(CollectionUtil.isNotEmpty(list)){
            reportDTO = list.get(0);
            reportDTO.setResultType(event_type);
            reportDTO.setResultTime(String.valueOf(System.currentTimeMillis()));
            reportDTO.setUpdateTime(new Date());
            reportService.update(reportDTO);
        }else{
            log.error("当前流水号，系统当前不存在");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        String callback = reportDTO.getCallback()+"&event_type="+event_type+"&timestamp="+ System.currentTimeMillis();

        log.info("七猫请求参数为{}",callback);
        String result = HttpUtil.get(callback);
        log.info("七猫响应结果为{}",result);

        JSONObject jsonObject = JSONObject.parseObject(result);

        if(ObjectUtils.allNotNull(jsonObject)){
            Integer code = jsonObject.getInteger("code");
            if(Objects.nonNull(code) && code.equals(0)){
                //调用结束后，移除这个值
                redisUtils.removeValueFromList(RedisCacheKey.qimao_callback_key,requestId);
            }
        }
        return ResponseEntity.ok(new Result());
    }

}
